package edu.ynu.se.xiecheng.achitectureclass.controller;

import edu.ynu.se.xiecheng.achitectureclass.service.SensorDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Api(tags = "传感器数据采集接口")
@RestController
@RequestMapping("/sensor-data")
public class SensorDataController {
    
    @Autowired
    private SensorDataService sensorDataService;
    
    @PostMapping("/collect")
    @ApiOperation("采集传感器数据")
    public ResponseEntity<?> collectData() {
        try {
            // 采集NPK数据
            sensorDataService.collectSensorData("300023040071", Arrays.asList(
                "327061330932536077",  // 土壤氮含量
                "327061330932536078",  // 土壤磷含量
                "327061330932536079"   // 土壤钾含量
            ));
            
            // 采集温湿度
            sensorDataService.collectSensorData("300023040071", Arrays.asList(
                "327061330933584659",  // 空气温度
                "327061330933584658"   // 空气相对湿度
            ));
            
            return ResponseEntity.ok(Map.of(
                "code", 200,
                "message", "数据采集成功"
            ));
        } catch (Exception e) {
            return ResponseEntity.ok(Map.of(
                "code", 400,
                "message", e.getMessage()
            ));
        }
    }

    @GetMapping("/latest")
    @ApiOperation("获取最新的传感器数据")
    public ResponseEntity<?> getLatestData(@RequestParam Long greenhouseId) {
        try {
            List<Map<String, Object>> latestData = sensorDataService.getLatestData(greenhouseId);
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "获取成功");
            response.put("data", latestData);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("code", 400);
            response.put("message", e.getMessage());
            return ResponseEntity.ok(response);
        }
    }

    @GetMapping("/history")
    @ApiOperation("获取历史数据")
    public ResponseEntity<?> getHistoryData(
            @RequestParam String serialNumber,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime) {
        try {
            List<Map<String, Object>> historyData = sensorDataService.getHistoryData(serialNumber, startTime, endTime);
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "获取成功");
            response.put("data", historyData);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("code", 400);
            response.put("message", e.getMessage());
            return ResponseEntity.ok(response);
        }
    }

    @GetMapping("/latest/{serialNumber}")
    @ApiOperation("获取单个传感器的最新数据")
    public ResponseEntity<?> getLatestDataBySerialNumber(@PathVariable String serialNumber) {
        try {
            Map<String, Object> latestData = sensorDataService.getLatestDataBySerialNumber(serialNumber);
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "获取成功");
            response.put("data", latestData);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("code", 400);
            response.put("message", e.getMessage());
            return ResponseEntity.ok(response);
        }
    }
} 